<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/22 0022
 * Time: 上午 9:41
 */
namespace app\admin\controller;


use app\admin\model\Producttype;
use app\common\model\Orderproductdetail;
use app\common\model\Printscene;
use app\common\service\PrinterTemplate;
use app\common\validate\printsConsole;
use app\lib\feieyun\Prints;
use think\Db;
use think\Request;


class PrinterConsole {

    private $businessId;

    public $printConfig = [
        '确认' => ['对帐单','出品单'],
        '改单' => ['对帐单','出品单'],
        '加单' => ['对帐单','出品单'],
        '买单' => ['结帐单']
    ];

    /*
     * 打印控制台
     */
    public function printsConsole($orderId=null,$storeId=null,$businessId = null, $isauto = false,$type = null, $goodsData = null){
        if(!$orderId || !$storeId){
            return show(config('status.ERROR_STATUS'),'缺少参数','缺少参数');
        }
        if($businessId){
            $this->businessId = $businessId;
        }else{
            if(session('TENANT_ID')){
                $this->businessId = session('TENANT_ID');
            }
        }

        $orderId = $orderId;
        $storeId = $storeId;
        $type = $type;
        $isauto = $isauto;



        $printModel = new \app\common\model\Printer();

        $printData = $printModel->where(['storeId'=>$storeId,'status'=>10])->field('group_concat(id) as idString')->find();
       if(!empty($printData->idString)){
           $tenant = \app\admin\model\Tenant::get($this->businessId);
           if (empty($type)){
               $type = '买单';
           }else{
               if($type == '达达配送'){
                   $printModel = $printModel->where(['storeId'=>$storeId,'status'=>10,'peisong_status'=>1])->select();
                   if($printModel){
                        foreach ($printModel as $value){
                            if(!empty($value['code']) && !empty($value['name'])){
                                $value['number'] = 1; //打印一次
                                $value['xiaopiaoname'] = '达达配送单';
                                $this->prints($orderId,$tenant->name,$value,$type,$value['type'],$goodsData);
                            }
                        }
                       return show(config('status.SUCCESS_STATUS'),'正在打印配送单','打印请求已发送');
                   }else{
                       return show(config('status.ERROR_STATUS'),'没有开启达达配送单','没有开启达达配送单');
                   }
               }
           }
           $PrintsceneModel = new Printscene();
           $Printscene = $PrintsceneModel->where('name','in',$this->printConfig[$type])->field('group_concat(id) as name')->find();

           //如果配置了打印场景
           if(!empty($Printscene->name)){
               if($isauto){
                   //如果是前台下单，需要判断是否开启了自动打印
                   $printModel = $printModel->where(['storeId'=>$storeId,'status'=>10,'isauto'=>1])->select();
                   if($printModel){
                       $model = Db::table('printerconfig')
                           ->join('printer','printer.id=printerconfig.printerId','left')
                           ->join('printscene','printscene.id=printerconfig.sceneId','left')
                           ->where('printer.isauto',1)
                           ->where('printerconfig.status',10)
                           ->where('printerconfig.printerId','in',explode(',',$printData->idString))
                           ->where('printerconfig.sceneId','in',explode(',',$Printscene->name))
                           ->field('printerconfig.*,printscene.name as name,printer.code as code,printer.type as printertype')->select();
                   }
               }else{
                   $model = Db::table('printerconfig')
                       ->join('printer','printer.id=printerconfig.printerId','left')
                       ->join('printscene','printscene.id=printerconfig.sceneId','left')
                       ->where('printerconfig.status',10)
                       ->where('printerconfig.printerId','in',explode(',',$printData->idString))
                       ->where('printerconfig.sceneId','in',explode(',',$Printscene->name))
                       ->field('printerconfig.*,printscene.name as name,printer.code as code,printer.type as printertype')->select();
               }

               //dump($model);exit;

               if(!empty($model)){
                   foreach ($model as $value){
                       if(!empty($value['code']) && !empty($value['name'])){
                           $this->prints($orderId,$tenant->name,$value,$type,$value['printertype'],$goodsData);
                       }
                   }
               }
           }
           return show(config('status.SUCCESS_STATUS'),'打印请求已发送','打印请求已发送');

       }else{
           return show(config('status.ERROR_STATUS'),'该门店没有配置打印机','没有配置打印机');
       }

    }


    private function prints($orderId,$name,$value,$type,$printtype,$goodsData){
        $html = '';
        $order = \app\common\model\Order::get($orderId)->toArray();
        if($type == '确认' || $type == '买单'){
            $goodsData = $this->getProducts($orderId, 'order');
            $html = $this->printsType($goodsData, $name, $value,$printtype,$order);
        }
        if($type == '达达配送'){
            $goodsData = $this->getProducts($orderId, 'order');
            $value['isdadaprint'] = 'yes';
            $html = $this->printsType($goodsData, $name, $value,$printtype,$order);
        }
        Prints::prints($value['code'],$value['number'],$html);
    }

    private function printsType($goodsData,$name,$value,$printtype,$order){
        $printtemp = new PrinterTemplate($goodsData, $name, $value, $printtype,$order);
        $html = $printtemp->printtemp();
        return $html;
    }

    //获取商品信息
    private function getProducts($orderId,$type,$goodsData=null){
        if($type == 'order'){
            $orderGoods = Orderproductdetail::where(['orderId'=>$orderId])->select();
            $order = \app\common\model\Order::get($orderId);

        }
        foreach ($orderGoods as $ordergood){
            $goods = \app\admin\model\Product::get($ordergood->productId);
            $categoryId = explode(',',$goods->typeId);
            $catgory = Producttype::where('id',$categoryId[0])->find();
            if(!empty($goods)){
                $data[] = [
                    'goodsId' => $goods->id,
                    'name' => $goods->title,
                    'image' => $goods->imgUrl,
                    'quantity' => $ordergood['count'],
                    'caiping' => $catgory->name,
                    'categoryId' => $catgory->id,
                    'price' => $ordergood->price,

                ];
            }

        }

        return $data;

    }



    public function index(){
        $this->printsConsole(1);
    }
}